import asyncio
import aiohttp
from lxml import etree


async def fetch(session, url, headers):
    async with session.get(url, headers=headers) as response:
        return await response.text()


async def process_book(session, book_id, headers):
    urls = f"https://huayu.zongheng.com/showchapter/{book_id}.html"
    html = await fetch(session, urls, headers)
    tree = etree.HTML(html)
    links = tree.xpath('//li[@class=" col-4"]/a/@href')
    titles = tree.xpath('//li[@class=" col-4"]/a/text()')
    h1 = tree.xpath('//h1/text()')
    print(f"章节: {titles}")
    print(h1)
    results = []
    for link in links:
        content_html = await fetch(session, link, headers)
        content_tree = etree.HTML(content_html)
        yue = content_tree.xpath('//div[@class="content"]/p/text()')
        results.append(yue)
    return results


async def main():
    num = 1
    url = 'https://www.zongheng.com/api2/catefine/storeSearch'
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0',
        'Cookie': 'ZHID=8F12792F68BB6B59D4FA7C121FF9C0A6; '
                  'v_user=https%3A%2F%2Fwww.zongheng.com%2F%7Chttps%3A%2F%2Fhuayu.zongheng.com%2F%7C22620529; '
                  'zh_visitTime=1744116244377; PassportCaptchaId=9c20e2f89a2664c46694a9ead8737803; zhffr=0; '
                  'sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22196156dd176112b-0674d4bc8d33f88-4c657b58'
                  '-2073600-196156dd177125e%22%2C%22%24device_id%22%3A%22196156dd176112b-0674d4bc8d33f88-4c657b58'
                  '-2073600-196156dd177125e%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4'
                  '%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22'
                  '%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6'
                  '%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; Hm_lvt_c202865d524849216eea846069349eb9=1744116244,1744200706; '
                  'HMACCOUNT=77A73498F77AD706; acw_tc=2760820217441971849596606e4963e9f47e06e9f5ae0969c4275df4a3c95b; '
                  'acw_sc__v2=67f6564079df05d9c2a4f75f9c359efbfed4ec95; '
                  'Hm_lpvt_c202865d524849216eea846069349eb9=1744201753'
    }

    while True:
        payload = {
            "worksTypes": 0,
            "bookType": 1,
            "subWorksTypes": 0,
            "totalWord": 0,
            "serialStatus": 1,
            "vip": 9,
            "pageNum": num,
            "pageSize": 20,
            "categoryId": 0,
            "categoryPid": 0,
            "naodongFilter": 0
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(url, headers=header, params=payload) as response:
                data = await response.json()
                book_list = data.get('result', {}).get('bookList', [])
                if not book_list:
                    break
                tasks = []
                for i in range(min(5, len(book_list))):
                    book_id = book_list[i]['bookId']
                    task = asyncio.create_task(process_book(session, book_id, header))
                    tasks.append(task)
                results = await asyncio.gather(*tasks)
                print("本轮五本书处理结果:", results)
        num += 1


if __name__ == "__main__":
    asyncio.run(main())